package com.qlmy.cn.ui.base

import android.content.Intent
import android.content.res.Resources
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ComponentActivity
import androidx.fragment.app.FragmentActivity
import androidx.viewbinding.ViewBinding
import by.kirich1409.viewbindingdelegate.viewBinding
import java.lang.reflect.ParameterizedType

typealias ActivityForResult = (ActivityResult) -> Unit
typealias RequestMultiplePermissions = (MutableMap<String, Boolean>) -> Unit

open class BaseActivity<VB : ViewBinding> : FragmentActivity(), View.OnClickListener {
    protected val viewBinding by viewBinding(::loadViewBinding)
    protected lateinit var mThis: BaseActivity<VB>
    private var mActivityForResult: ActivityForResult? = null
    private var mIsNeedResultOk: Boolean? = null
    private var mRequestMultiplePermissions: RequestMultiplePermissions? = null
    private val mStartActivityForResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult ->
        if (mIsNeedResultOk == true) {
            if (activityResult.resultCode == RESULT_OK)
                mActivityForResult?.invoke(activityResult)
        } else {
            mActivityForResult?.invoke(activityResult)
        }
    }
    private val mRequestMultiplePermissionsLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { activityResult ->
        mRequestMultiplePermissions?.invoke(activityResult)
    }

    fun bindViewClick(vararg views: View) = com.qlmy.cn.commom.bindViewClick(this, *views)


    private fun loadViewBinding(view: ComponentActivity): VB {
        val clz = (this::class.java.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<ViewBinding>
        val method = clz.getDeclaredMethod("inflate", LayoutInflater::class.java)
        return method.invoke(null, layoutInflater) as VB
    }

    open fun loadView(load: VB.() -> Unit) = viewBinding.also(load)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //com.yanzhenjie.sofia.Utils.setStatusBarDarkFont(this, true)
        mThis = this
        setContentView(viewBinding.root)
        init()
    }

    private fun init() {
        initView()
        initData()
    }

    open fun initView() {}
    open fun initData() {}
    open fun doFinish() {
        finish()
    }

    override fun onClick(v: View?) {

    }

    open fun launchStartActivityForResult(clazz: Class<*>, options: (Bundle.() -> Unit)? = null, isNeedResultOk: Boolean = true, activityForResult: ActivityForResult? = null) {
        val intent = Intent(this, clazz)
        options?.let { intent.putExtras(Bundle().also(it)) }
        mActivityForResult = activityForResult
        mIsNeedResultOk = isNeedResultOk
        mStartActivityForResultLauncher.launch(intent)
    }

    open fun launchRequestMultiplePermissions(permissions: Array<String>, requestMultiplePermissions: RequestMultiplePermissions? = null) {
        mRequestMultiplePermissions = requestMultiplePermissions
        mRequestMultiplePermissionsLauncher.launch(permissions)
    }

    override fun getResources(): Resources {
        //AutoSizeCompat.autoConvertDensityOfGlobal((super.getResources()))
        return super.getResources()
    }
}